package caches;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;


public class SynchCache<K, V> implements Cache<K, V> {
	private final AtomicReference<K> key = new AtomicReference<>();
	private final AtomicReference<V> value = new AtomicReference<>();
	ReadWriteLock lock = new ReentrantReadWriteLock();

	@Override
	public synchronized V get(K key) {
		//lock.readLock().lock();
		try {
			if (this.key.get() != null && this.key.get().equals(key))
				return value.get();
			else
				return null;
		} finally {
			//lock.readLock().unlock();
		}
	}

	@Override
	public synchronized void put(K key, V value) {
		//lock.writeLock().lock();
		try {
			this.key.set(key);
			this.value.set(value);
		} finally {
			//lock.writeLock().unlock();
		}
	}
}
